package cool.mor.service.impl;

import cool.mor.dao.AttendanceRepository;
import cool.mor.dao.DepartmentRepository;
import cool.mor.dao.EmployeeRepository;
import cool.mor.dao.RewardsAndPunishmentRepository;
import cool.mor.dto.SalaryManagementPageQueryDto;
import cool.mor.dto.SalaryManagementSalaryQueryDto;
import cool.mor.entity.Attendance;
import cool.mor.entity.Department;
import cool.mor.entity.Employee;
import cool.mor.entity.RewardsAndPunishment;
import cool.mor.form.SalaryManagementPageQueryForm;
import cool.mor.service.SalaryManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Ivan
 */
@Service
public class SalaryManagementServiceImpl implements SalaryManagementService {
    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    DepartmentRepository departmentRepository;
    @Autowired
    RewardsAndPunishmentRepository rewardsAndPunishmentRepository;
    @Autowired
    AttendanceRepository attendanceRepository;
    @Override
    public SalaryManagementPageQueryDto querySalaryManagementDataByPage(SalaryManagementPageQueryForm salaryManagementPageQueryForm) {
        SalaryManagementPageQueryDto salaryManagementPageQueryDto = new SalaryManagementPageQueryDto();
        int i=1;
        /**
         * 在dao中查询出page对象
         *
         */
        if (Integer.parseInt(salaryManagementPageQueryForm.getPage())>0){
            Pageable pageable = PageRequest.of(Integer.parseInt(salaryManagementPageQueryForm.getPage())-i,Integer.parseInt(salaryManagementPageQueryForm.getSize()));
            Page<Employee> employeePage = employeeRepository.findAll(pageable);
            salaryManagementPageQueryDto.setPageData(employeePage);
            salaryManagementPageQueryDto.setCode("200");
            salaryManagementPageQueryDto.setMessage("查找成功");
            return salaryManagementPageQueryDto;
        }
        salaryManagementPageQueryDto.setCode("301");
        salaryManagementPageQueryDto.setMessage("查找失败");
        return salaryManagementPageQueryDto;
    }

    @Override
    public SalaryManagementSalaryQueryDto querySalaryManagementSalary(String employeeId) {
        /**
         *   1.查询部门名称  奖惩总额  薪资总和   缺勤记录
         */
        SalaryManagementSalaryQueryDto salaryManagementSalaryQueryDto = new SalaryManagementSalaryQueryDto();

        Integer rewardsScoreTotal=0;
        Integer salaryTotal=0;
        Integer attendanceTotal=0;
        Department department = departmentRepository.findById(employeeRepository.findById(employeeId).get().getDeptNo()).get();
        String baseSalary = department.getBaseSalary();
        String departmentName = department.getDepartmentName();
        List<RewardsAndPunishment> allByEmployeeId = rewardsAndPunishmentRepository.findAllByEmployeeId(employeeId);
        for (RewardsAndPunishment rewardsAndPunishment: allByEmployeeId
        ) {
            rewardsScoreTotal+=Integer.parseInt(rewardsAndPunishment.getRewardsAndPunishmentScore());
        }
        salaryTotal=Integer.parseInt(baseSalary)+rewardsScoreTotal;
        salaryManagementSalaryQueryDto.setRewardsAndPunishmentsTotal(rewardsScoreTotal.toString());
        salaryManagementSalaryQueryDto.setSalaryTotal(salaryTotal.toString());
        salaryManagementSalaryQueryDto.setDepartmentName(departmentName);
        salaryManagementSalaryQueryDto.setCode("200");
        salaryManagementSalaryQueryDto.setMessage("succeed");
        /**
         * 根据员工id查询考勤表，计算缺勤次数总和
         */
        List<Attendance> allAttendance = attendanceRepository.findAllByEmployeeId(employeeId);
        for (Attendance attendance : allAttendance) {
            if ("缺勤".equals(attendance.getMorningAttendanceType())) attendanceTotal+=1;
            if ("缺勤".equals(attendance.getAfternoonAttendanceType())) attendanceTotal+=1;
        }
        salaryManagementSalaryQueryDto.setAbsenceRecords(attendanceTotal.toString());
        return salaryManagementSalaryQueryDto;
    }
}
